<template>
  <div class="main">
    <van-nav-bar fixed>
      <template v-slot:left>
        <img class="logo" src="@/assets/logo.png" alt="" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" color="#fff" @click="moveSearchPageFn" />
      </template>
    </van-nav-bar>
    <div>
      <van-tabs
        v-model="channelId"
        animated
        sticky
        offset-top="1.226667rem"
        @change="channelChangeFn"
      >
        <van-tab
          v-for="obj in userChannelList"
          :key="obj.id"
          :title="obj.name"
          :name="obj.id"
        >
          <ArticleList :channelId="channelId"></ArticleList>
        </van-tab>
      </van-tabs>
      <!-- 编辑频道图标 -->
      <van-icon
        name="plus"
        size="0.37333334rem"
        class="moreChannels"
        @click="plusClickFn"
      />
    </div>
    <!-- 频道管理弹出层 -->
    <van-popup v-model="show" class="channel_popup">
      <ChannelEdit
        :userList="userChannelList"
        :unClickList="unCheckChannelList"
        @addChannelEV="addChannelFn"
        @delChannelEV="delChannelFn"
        @closeEV="closeFn"
        ref="editRef"
        v-model="channelId"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannelsAPI, getAllChannelsAPI, updateChannelsAPI, removeChannelsAPI } from '@/api'
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './ChannelEdit.vue'
export default {
  data () {
    return {
      channelId: 0, // tab导航-激活频道id(默认频道id为0, 页面刚打开是推荐频道高亮-对应文章数据)
      userChannelList: [], // 用户选择频道列表
      allChannelList: [], // 所有频道列表
      // articleList: [] // 文章列表
      show: false, // 频道管理弹出层隐藏
      channelScrollTObj: {} // 保存每个频道的滚动位置
    }
  },
  components: {
    ArticleList,
    ChannelEdit
  },
  async created () {
    // 获取登录用户频道列表
    const res = await getUserChannelsAPI()
    // console.log(res)
    this.userChannelList = res.data.data.channels
    // this.channelChangeFn()

    // 所有频道列表
    const res2 = await getAllChannelsAPI()
    // console.log(res2)
    this.allChannelList = res2.data.data.channels
  },
  methods: {
    // tab切换的事件 -> 获取文章列表数据
    async channelChangeFn () {
      // // 文章列表
      // const res = await getAllArticleListAPI({
      //   channel_id: this.channelId,
      //   timestamp: new Date().getTime()
      // })
      // // console.log(res)
      // this.articleList = res.data.data.results
      // }

      // tab切换后,设置滚动位置
      // tab切换时, 这个组件内部, 会把切走的容器height
      // 切回来一瞬间, 没有高度, 滚动事件从地下上来也
      // 切换来的一瞬间, 高度位0, 你设置滚动位置也没用
      this.$nextTick(() => {
        document.documentElement.scrollTop = this.channelScrollTObj[this.channelId]
        document.body.scrollTop = this.channelScrollTObj[this.channelId]
      })
    },
    // 点击+,频道管理弹出层显示
    plusClickFn () {
      this.show = true
    },
    // 添加频道
    async addChannelFn (channelObj) {
      this.userChannelList.push(channelObj)

      const newArr = this.userChannelList.filter(obj => obj.id !== 0)
      const theNewArr = newArr.map((obj, index) => {
        const newObj = { ...obj } // 拷贝(浅拷贝)
        delete newObj.name
        newObj.seq = index
        return newObj // 让map方法把新对象收集起来形成一个新数组
      })
      const res = await updateChannelsAPI({
        channels: theNewArr
      })
      console.log(res)

      // const res = await updateChannelsAPI(this.userChannelList)
      // console.log(res)
    },
    // 删除频道
    async delChannelFn (channelObj) {
      const index = this.userChannelList.findIndex(obj => obj.id === channelObj.id)
      this.userChannelList.splice(index, 1)
      const res = await removeChannelsAPI(channelObj.id)
      console.log(res)
      // 删除接口返回状态码204(Not Content) 无返回内容
    },
    // 关闭弹出层
    closeFn () {
      this.show = false
      // 让内部编辑状态回归false
      this.$refs.editRef.isEdit = false
    },
    // 首页-右上角放大镜点击事件 -> 跳转搜索页面
    moveSearchPageFn () {
      this.$router.push('/search')
    },
    // 监听网页滚动事件
    scrollFn () {
      this.$route.meta.scrollT = document.documentElement.scrollTop || document.body.scrollTop
      // 同时保存当前频道的滚动距离
      this.channelScrollTObj[this.channelId] = document.documentElement.scrollTop || document.body.scrollTop
    }
  },
  computed: {
    unCheckChannelList () {
      // 目标: 把所有频道数组挨个对象取出, 去用户已选频道数组里查找, 如果找不到, 则让filter方法收集到一个新数组里
      const newArr = this.allChannelList.filter((bigObj) => {
        const index = this.userChannelList.findIndex((smallObj) => {
          return smallObj.id === bigObj.id
        })

        // 如果找到了
        if (index > -1) {
          return false
        } else {
          return true
        }
      })
      return newArr
    }
  },
  // 只有使用keep-alive的组件才有这两个生命周期
  activated () { // 切回来
    console.log(this.$route)
    window.addEventListener('scroll', this.scrollFn)
    // window和document, 监听网页滚动事件
    // html标签获取scrollTop, 滚动的距离, 和设置滚动的位置
    // 立刻设置, 滚动条位置
    document.documentElement.scrollTop = this.$route.meta.scrollT
    document.body.scrollTop = this.$route.meta.scrollT
  },
  deactivated () { // 切走
    window.removeEventListener('scroll', this.scrollFn)
  }
  // 先切走了, 滚动回调顶部, 才触发deactivated失焦, 所以拿不到滚动位置了
}
</script>

<style scoped lang="less">
.logo {
  width: 100px;
  height: 30px;
}
.main {
  padding-top: 46px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
.channel_popup {
  width: 100vw;
  height: 100vh;
}
</style>
